package web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;

import utils.CCItemOrdenDeCompra;
import utils.CCOrdenDeCompra;



import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

import entidades.Cliente;
import vo.CCSolicitudCotizacionOperadorVO;

import vo.CCSolicitudCotizacionVO;
import facade.BusinessDelegateBean;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;


public class ServletCompra extends HttpServlet implements
		javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;

	private List <CCItemOrdenDeCompra> listaItems=new ArrayList <CCItemOrdenDeCompra>();
	
	private boolean inicializado = false;//flag

	public ServletCompra() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession(true);
		String action = (String) request.getParameter("action");
		
		
		CCItemOrdenDeCompra item;
		if (action.equals("Agregar Item Compra")) {
			System.out.println("entre al agregar item de compra del servlet");
			try {
				item = new CCItemOrdenDeCompra();
				//si este no es el primer elemento
				if (this.inicializado == true) {
					this.listaItems = (List<CCItemOrdenDeCompra>) session.getAttribute("itemsCompra");
					System.out.println("me traigo el vector del carrito de items compra ya agregadas. size: "+this.listaItems.size());
				}
				else{
					System.out.println("creo un nuevo vector");
					this.listaItems =new ArrayList<CCItemOrdenDeCompra>();
				}
					
					item.setCantidad(Integer.parseInt(request.getParameter("cantRod")));
					item.setSKF(request.getParameter("codigoRod"));
					item.setMarca(request.getParameter("marcaRod"));
					item.setPais(request.getParameter("paisRod"));
					
					//Imprimo en pantalla los valores
					System.out.println("Cant: "+item.getCantidad());
					System.out.println("Codigo: "+item.getSKF());
					System.out.println("Marca: "+item.getMarca());
					this.listaItems.add(item);
					
					
					session.setAttribute("itemsCompra", this.listaItems);

					this.inicializado=true;
					//redireccionamiento
					RequestDispatcher dispatcher = request.getRequestDispatcher("/solicitudOrdenCompra.jsp");
					dispatcher.forward(request, response);
			}

			catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (action.equals("Enviar")) {
			try {
				
				this.listaItems = (List<CCItemOrdenDeCompra>) session.getAttribute("itemsCompra");
				CCOrdenDeCompra root= new CCOrdenDeCompra();
				//root.setIdCliente();
				root.setItems(this.listaItems);
				System.out.println(" size del vector antes de enviar: "+this.listaItems.size());
				//XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6
				//xstream.alias("CCSolicitudCotizacionParentVO", CCSolicitudCotizacionParentVO.class);
				
				//String xml = xstream.toXML(root);
				//System.out.println(xml);
				
				//session.setAttribute("xml", xml);
				//redireccionamiento
				
				BusinessDelegateBean bd=new BusinessDelegateBean();
				Cliente c= new Cliente();
				c.setId(Integer.valueOf(request.getParameter("idCliente")));
				bd.enviarOrdenDeCompra(root, c);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
				dispatcher.forward(request, response);
				
				
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

}
